package com.mvd.app.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.mvd.app.R;
import com.mvd.app.model.BuHeGeModel3;
import com.mvd.app.model.MyFlagModel;
import com.mvd.app.model.OtherWaiJianPhotoModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 **/
public class OtherWaiJianPhotoAdapter extends BaseAdapter {

    private static final String TAG = "tag";
    private List<OtherWaiJianPhotoModel> mContentList;
    private Context context;
    private LayoutInflater mInflater;
    private MyClickListener mListener;

    public static Map<Integer, Boolean> map = new HashMap<>();// 存放已被选中的CheckBox

    public OtherWaiJianPhotoAdapter(Context context, List<OtherWaiJianPhotoModel> contentList,
                                    MyClickListener listener) {
        mContentList = contentList;
        mInflater = LayoutInflater.from(context);
        mListener = listener;
        this.context = context;
        init();
    }

    // 初始化
    private void init() {
        // 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
        if (mContentList != null && mContentList.size() > 0) {
            map = new HashMap<Integer, Boolean>();
            for (int i = 0; i < mContentList.size(); i++) {
                map.put(i, false);
            }
        }
    }

    @Override
    public int getCount() {
        Log.i(TAG, "getCount");
        return mContentList.size();
    }

    @Override
    public Object getItem(int position) {
        Log.i(TAG, "getItem");
        return mContentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.i(TAG, "getItemId");
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.i(TAG, "getView");
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_other_waijian_photo, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView
                    .findViewById(R.id.tv_item_photo);
            holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.cb_item_photo);
//            holder.msg = (TextView) convertView
//                    .findViewById(R.id.item_ac_waijian_tv_msg);
//            holder.button = (Button) convertView.findViewById(R.id.item_ac_waijian_btn_luntai_guige_r_b_preview);
//            holder.button2 = (Button) convertView.findViewById(R.id.item_ac_waijian_btn_luntai_guige_r_b);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(mContentList.get(position).getName());
////        holder.button.setOnClickListener(mListener);
//        holder.button.setOnClickListener(mListener);
//        holder.button.setTag(new MyFlagModel(position, 0));
//        holder.button2.setOnClickListener(mListener);
//        holder.button2.setTag(new MyFlagModel(position, 1));

//        int color = mContentList.get(position).getColor();
//        holder.msg.setText(mContentList.get(position).getName());
//        if (color == 0) {
////            holder.tv2.setTextColor(context.getResources().getColor(R.color.red));
//        } else if (color == 1) {
//            holder.textView.setTextColor(context.getResources().getColor(R.color.textcolor_green));
//            holder.textView.setText("已拍照");
//        }

        holder.mCheckBox.setChecked(map.get(position));
//        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if(isChecked==true){
//                    map.put(position,true);
//                }else {
//                    map.remove(position);
//                }
//            }
//        });
//
//        if(map!=null&&map.containsKey(position)){
//            holder.mCheckBox.setChecked(true);
//        }else {
//            holder.mCheckBox.setChecked(false);
//        }
        return convertView;
    }

    public class ViewHolder {
        public TextView textView, msg;
        //        public Button button, button2;
        public CheckBox mCheckBox;
    }

    /**
     * 用于回调的抽象类
     *
     * @author Ivan Xu
     *         2014-11-26
     */
    public static abstract class MyClickListener implements OnClickListener {
        /**
         * 基类的onClick方法
         */
        @Override
        public void onClick(View v) {
//            myOnClick((Integer) v.getTag(), v);
            myOnClick(((MyFlagModel) v.getTag()).getPosition(), v, ((MyFlagModel) v.getTag()).getFlag());
        }

        public abstract void myOnClick(int position, View v, int flag);
    }

    public Map<Integer, Boolean> getMap() {
        return map;
    }

    public void setMap(Map<Integer, Boolean> map) {
        OtherWaiJianPhotoAdapter.map = map;
    }
}